﻿// Autore: Mattia Barrasso

using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.InputDevices;
using White.Core.UIItems.Finders;
using ListView = White.Core.UIItems.ListView;

namespace RecipesManager.Specs.Negozio
{
    [Binding, Scope(Feature = "ModificaNegozio")]
    class ModificaNegozio : NegoziBDD
    {
        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [Given(Regex = "il negozio ([a-zA-Z]+) non esiste")]
        public void DatoNegozioNonEsistente(string negozio)
        {
            Assert.That(!Manager.Shops.Contains(negozio));
        }

        [Given(Regex = "il negozio ([a-zA-Z]+) esiste")]
        public void DatoNegozioEsistente(string negozio)
        {
            Manager.Shops.Add(negozio);
            Assert.That(Manager.Shops.Contains(negozio));
        }

        [Given(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void DatoIngredienteEsistente(string ingrediente)
        {
            Manager.Ingredients.Add(ingrediente, MeasureType.Weight);
            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        //TODO @EMa
        [Given(@"rimuovo l'ingrediente ([a-zA-Z]+)")]
        public void DatoRimuovoLIngredienteCicoria(string ingrediente)
        {
            var lw = (ListView)SelectedIngredients;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {

                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(ingrediente));
                if (element == null) continue;
                //var checkBox = lw.Rows[i].Get<White.Core.UIItems.CheckBox>(SearchCriteria.All);
                //checkBox.Toggle();
                //(?) Se non ci fosse stato un UIITem?!
                //Alternative
                //ClickablePoint? Scatenare il click date le coord.
                Mouse.instance.Location = element.GetClickablePoint();
                Mouse.instance.Click();
                break;
            }
            Assert.That(i != lw.Rows.Count);
            RemoveIngredient.Click();
        }

        [Given(Regex = @"il negozio ([a-zA-Z]+) ha i valori")]
        public void DatiValoriNegozio(string negozio, Table table)
        {
            var address = Manager.Shops[negozio].Address;
            var city = Manager.Shops[negozio].City;
            var telephone = Manager.Shops[negozio].PhoneNumber;
            var timetable = Manager.Shops[negozio].Timetable;
            foreach (var row in table.Rows)
            {
                if (row["Attributi"].Equals("Indirizzo"))
                {
                    address = row["Valori"];
                    Manager.Shops[negozio].Update(negozio, address, city, telephone, timetable);
                }
                else if (row["Attributi"].Equals("Citta"))
                {
                    city = row["Valori"];
                    Manager.Shops[negozio].Update(negozio, address, city, telephone, timetable);
                }
                else if (row["Attributi"].Equals("Telefono"))
                {
                    telephone = row["Valori"];
                    Manager.Shops[negozio].Update(negozio, address, city, telephone, timetable);
                }
                else if (row["Attributi"].Equals("Orario"))
                {
                    timetable = row["Valori"];
                    Manager.Shops[negozio].Update(negozio, address, city, telephone, timetable);
                }
                else if (row["Attributi"].Equals("Ingrediente"))
                {
                    Assert.That(Manager.Ingredients.Contains(row["Valori"]));
                    Manager.Shops[negozio].IngredientRatings.Add(row["Valori"], ShopRating.Bad);
                }
                else
                {
                    Assert.Fail("Table");
                }
            }           
        }

        [Given(Regex = "seleziono il negozio ([a-zA-Z]+)")]
        public void DatoSelezionoNegozio(string negozio)
        {
            base.NavigateToShops();
            base.GetShopsUi();
            Assert.That(Shops.Items.Exists(shop => shop.Text.Equals(negozio)));
            Shops.Select(negozio);
            base.GetSingleShopUi();
        }

        [Given(Regex = @"il negozio ([a-zA-Z]+) ha ([0-9]+) ingredienti")]
        public void DatoNegozioHaNIgredienti(string negozio, int nIngredienti, Table table)
        {
            Assert.That(table.Rows.Count == nIngredienti);
            foreach (var row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    Manager.Shops[negozio].IngredientRatings.Add(row["Valori"], ShopRating.Good);
                    Assert.That(Manager.Shops[negozio].IngredientRatings.Contains(row["Valori"]));
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [Given(Regex = "ho inserito i valori")]
        public void DatoInseritoNegozio(Table table)
        {
            foreach (var row in table.Rows)
            {
                if (row["Attributi"].Equals("Nome"))
                {
                    ShopName.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Indirizzo"))
                {
                    ShopAddress.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Citta"))
                {
                    ShopCity.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Telefono"))
                {
                    ShopTelephone.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Orario"))
                {
                    ShopOpeningTimes.SetValue(row["Valori"]);
                }
                else if (row["Attributi"].Equals("Ingrediente"))
                {
                    Assert.That(Manager.Ingredients.Contains(row["Valori"]));
                    Ingredients.Click();
                    Ingredients.Select(row["Valori"]);
                    AddIngredient.Click();
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [When(Regex = "salvo")]
        public void QuandoSalvo()
        {
            Assert.That(SaveShop.Enabled);
            SaveShop.Click();
        }

        [Then(Regex = "l'operazione non ha successo")]
        public void AlloraOperazioneInsuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count != 0 || !SaveShop.Enabled);
        }

        [Then(Regex = "l'operazione ha successo")]
        public void AlloraOperazioneSuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count == 0);
        }

        [Then(Regex = "il negozio ([a-zA-Z]+) esiste")]
        public void AlloraNegozioEsiste(string negozio)
        {
            Assert.That(Shops.Items.Exists(item => item.Text.Equals(negozio)));
            //NavigateToMainMenu();
            //NavigateToShops();
            Manager.ClearCache();
            Assert.That(Manager.Shops.Contains(negozio));
        }

        [Then(Regex = "il negozio ([a-zA-Z]+) non esiste")]
        public void AlloraNegozioNonEsiste(string negozio)
        {
            Assert.That(!Shops.Items.Exists(item => item.Text.Equals(negozio)));
        }

        
        [Then(Regex = @"il negozio ([a-zA-Z]+) ha ([0-9]+) ingredienti")]
        public void AlloraNegozioHaNIgredienti(string negozio, int nIngredienti, Table table)
        {
            Assert.That(table.Rows.Count == nIngredienti && nIngredienti == Manager.Shops[negozio].IngredientRatings.Count);
            foreach (var row in table.Rows)
            {
                if(row["Attributi"].Equals("Nome"))
                {
                    Assert.That(Manager.Shops[negozio].IngredientRatings.Contains(row["Valori"]));
                }else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [Then(Regex = @"il negozio ([a-zA-Z]+) ha i valori")]
        public void AlloraValoriNegozio(string negozio, Table table)
        {
            //BackToMainMenu.Click();
            Assert.That(Manager.Shops.Contains(negozio));
            foreach (var row in table.Rows)
            {
                if (row["Attributi"].Equals("Indirizzo"))
                {
                    var address = Manager.Shops[negozio].Address;
                    Assert.That(row["Valori"].Equals(Manager.Shops[negozio].Address));
                }
                else if (row["Attributi"].Equals("Citta"))
                {
                    Assert.That(row["Valori"].Equals(Manager.Shops[negozio].City));
                }
                else if (row["Attributi"].Equals("Telefono"))
                {
                    Assert.That(row["Valori"].Equals(Manager.Shops[negozio].PhoneNumber));
                }
                else if (row["Attributi"].Equals("Orario"))
                {
                    Assert.That(row["Valori"].Equals(Manager.Shops[negozio].Timetable));
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

    }
}
